﻿<%@ Page Title="" Language="C#" MasterPageFile="MemberMaster.Master" AutoEventWireup="true"
    CodeBehind="ShoppingCart.aspx.cs" Inherits="WinnerCMS.WebUI.Templates.WinnerCMS6.Member.ShoppingCart" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="WinnerCMS.BLL" %>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        $(function ()
        {
            In();
        });

        function In()//每行小计
        {
            $.ajax({ type: "POST",
                url: "/AjaxPostBack.ashx?Action=LoadShopingCartData",
                dataType: "JSON",
                error: function (XMLHttpRequest, textStatus, errorThrown)
                {
                    alert(XMLHttpRequest.statusText);

                },
                success: function (info)
                {
                    for (var i = 0; i < info.length; i++)
                    {
                        var temp = "<tr style=\"height: 24px;\"><td><a href=\"/ShopShow-@GoodsID@.htm\" target=\"_blank\">@ContentTitle@</a></td><td align=\"right\" style=\"width: 180px;\">@PriceMember@</td><td align=\"right\" class=\"PriceMember\" style=\"width: 80px;\">@PriceMember_Cuted@</td><td align=\"right\" style=\"width: 80px;\"><input type=\"text\" class=\"Quantity\" value=\"@Quantity@\" style=\"width: 99%; text-align: right;\" onkeyup=\"EditShoppingCartQuantity(@ShopingCartID@,this)\" /></td><td align=\"right\" style=\"width: 80px;\"><span class=\"Subtotal\">0</span></td><td align=\"center\" style=\"width: 60px;\"><input onclick=\"Delete(@ShopingCartID@,this)\" type=\"button\" class=\"btn\" value=\"删除\" /></td></tr>";

                        temp = temp.replace("@ShopingCartID@", info[i].Id);
                        temp = temp.replace("@ShopingCartID@", info[i].Id);
                        temp = temp.replace("@GoodsID@", info[i].GoodsID);
                        temp = temp.replace("@ContentTitle@", info[i].ShopInfo.ContentTitle);
                        temp = temp.replace("@PriceMember@", info[i].ShopInfo.PriceMember);
                        temp = temp.replace("@PriceMember_Cuted@", info[i].PriceMember_Cuted);
                        temp = temp.replace("@Quantity@", info[i].Quantity);

                        $("#cart_table tbody").append(temp);
                    }

                    $("#cart_table tbody tr").each(function (i)
                    {
                        var Price = $(this).find(".PriceMember").html();
                        var Quantity = $.trim($(this).find(".Quantity").val());
                        $(this).find(".Subtotal").html((Price * Quantity).toFixed(2));
                    });

                    AllTotal();
                }
            });
        }
        function AllTotal()
        {
            var itemsQuantity = 0;
            var itemsTotal = 0;
            $("#cart_table tbody tr").each(function (i)
            {
                if ($.trim($(this).find(".PriceMember").html()) != "")
                {
                    var Price = $.trim($(this).find(".PriceMember").html());
                    var Quantity = $.trim($(this).find(".Quantity").val());

                    itemsTotal += parseFloat(Price) * parseFloat(Quantity);
                    itemsQuantity += parseFloat(Quantity);
                }
            });
            $("#total_price").html(fomatFloat(itemsTotal, 2));
            $("#total_quantity").html(itemsQuantity);
            //如果购物车为空的情况下进行相关操作

        }

        function EditShoppingCartQuantity(_ID, input_Quantity)
        {
            var re = /^[1-9]+[0-9]*]*$/;
            if (!re.test(input_Quantity.value))
            {
                alert("填入数据要求是大于0的整数");
                input_Quantity.value = 1
                return;
            }
            $.ajax({ type: "POST",
                url: "/AjaxPostBack.ashx?Action=EditShoppingCartQuantity",
                dataType: "JSON",
                data: {
                    ID: _ID,
                    Quantity: input_Quantity.value
                },
                success: function (info)
                {
                    if (info.errors == "")
                    {
                        var tr = $(input_Quantity).parent().parent();

                        var Price = parseFloat(tr.find(".PriceMember").html());
                        var Quantity = parseFloat(tr.find(".Quantity").val());

                        tr.find(".Subtotal").html((Price * Quantity).toFixed(3));

                        AllTotal();
                    }
                    else
                    {
                        alert(info.errors);
                    }
                }
            });
        }

        function Delete(_ID, obj)
        {
            if (confirm('您确定要删除？') == false)
            {
                return false;
            }
            if (_ID > 0 && obj.parentNode.parentNode.tagName == "TR")
            {
                $.ajax({ type: "POST",
                    url: "/AjaxPostBack.ashx?Action=ShopingCartDelete",
                    dataType: "JSON",
                    data: { ID: _ID },
                    success: function (info)
                    {
                        if (info.errors == "")
                        {
                            var TR = obj.parentNode.parentNode;

                            TR.removeChild();
                            AllTotal();
                        }
                        else
                        {
                            alert(info.errors);
                        }
                    }
                });
            }
            return true;
        }
        function fomatFloat(src, pos)
        {
            return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
        }
  
    </script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <form id="Form1" runat="server">
    <table cellspacing="0" align="center" rules="all" border="1" id="cart_table" style="width: 100%;
        border-collapse: collapse;">
        <thead>
            <tr style="color: White; background-color: Black; font-size: 12px; font-weight: bold;
                height: 24px;">
                <th align="center" scope="col">
                    商品名称
                </th>
                <th scope="col">
                    价格
                </th>
                <th scope="col">
                    价格(折扣后)
                </th>
                <th scope="col">
                    数量
                </th>
                <th scope="col">
                    小计
                </th>
                <th scope="col">
                    &nbsp;
                </th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <div style="text-align: right; margin-top: 20px;">
        <b>数量: </b><span id="total_quantity"></span>
        <br />
        <b>合计: </b><span id="total_price"></span>
    </div>
    <div style="text-align: left; margin-top: 20px;">
        <asp:Button ID="btnClear" runat="server" Text="清空" CssClass="btn" />
        <%-- <input id="btnContinue" type="button" value="继续购物" onclick="window.close();opener.focus();"
                    class="btn" />--%>
        <asp:Button ID="btnNext" runat="server" Text="购买" CssClass="btn" />
    </div>
    </form>
</asp:Content>
